package threshold;

import java.util.Stack;

/**
 * @Author wujiaming
 * @Description TODO
 * @Date 2023-03-07 17:36
 * @Version 1.0
 */
public class MInDepth {


    public void postOder(TreeNode root){
        TreeNode[] stack1 = new TreeNode[50];
        TreeNode p = root;
        int[] stack2 = new int[50];
        int top = -1,falg;
        while(p!=null || top!=-1){
            while(p!=null){
                stack1[++top] = p;
                stack2[top] = 0;
                p = p.left;
            }
            p = stack1[top];
            falg =stack2[top--];
            if(falg==0){
                stack1[++top] = p;
                stack2[top] = 1;
                p = p.right;
            }else{
                System.out.println(p.val);
                p = null;
            }
        }
    }
     public int minDepth(TreeNode root) {
         int min = Integer.MAX_VALUE;
         if(root==null){return 0;}


        TreeNode[] stack1 = new TreeNode[50];
        TreeNode p = root;
        int[] stack2 = new int[50];
        int top = -1,falg;
        while(p!=null || top!=-1){
            while(p!=null){
                stack1[++top] = p;
                stack2[top] = 0;
                p = p.left;
            }
            p = stack1[top];
            falg =stack2[top--];
            if(falg==0){
                stack1[++top] = p;
                stack2[top] = 1;
                p = p.right;
            }else{
                if(p.left==null && p.right==null){
                    if(top+2<=min){
                        min = top+2;
                    }
                }
                p = null;
            }
        }
         return min;
    }
    public static void main(String[] args) {
        MInDepth test = new MInDepth();
        TreeNode root = new TreeNode(3);
        TreeNode node1 = new TreeNode(9);
        TreeNode node2 = new TreeNode(20);
        TreeNode node3 = new TreeNode(15);
        TreeNode node4 = new TreeNode(7);
        root.left=node1;root.right=node2;
        node2.left=node3;node2.right=node4;
        System.out.println(test.minDepth(root));


    }
}
